<template>
  <div>
    <div class="coupon-detail">
      <el-select
        v-model="status"
        clearable
        @change="statusChange()"
      >
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </div>
    <el-table
      :data="dataList"
      style="width: 100%"
      header-cell-class-name="table-header"
      row-class-name="table-row-low"
      class="user-edit-table"
    >
      <el-table-column
        prop="receiveTime"
        :label="$t('user.getCouponTime')"
      />
      <el-table-column :label="$t('marketing.couponName')">
        <template #default="scope">
          <div>
            <span>{{ scope.row.coupon.couponName }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column :label="$t('coupon.couponType')">
        <template #default="scope">
          <span class="tag-text">{{
            [$t('coupon.cashCoupon'), $t('coupon.discountVoucher'), $t('coupon.coinCertificate')][
              scope.row.coupon.couponType - 1
            ]
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="score"
        :label="$t('user.effectiveTime')"
      >
        <template #default="scope">
          <div>
            <span>{{ scope.row.userStartTime }}{{ $t('text.to') }}{{ scope.row.userEndTime }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        :label="$t('user.couponStatus')"
        width="100"
      >
        <template #default="scope">
          <span>{{
            [$t('user.invalid'), $t('user.notUsed'), $t('user.used')][scope.row.status]
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('user.userRules')"
        width="150"
      >
        <template #default="scope">
          <div v-if="scope.row.coupon.couponType === 1">
            <span>{{ $t('marketing.full') }}{{ scope.row.coupon.cashCondition }}{{ $t('coupon.yuan')
            }}{{ $t('marketing.reducea') }}{{ scope.row.coupon.reduceAmount
            }}{{ $t('coupon.yuan') }}</span>
          </div>
          <div v-if="scope.row.coupon.couponType === 2">
            <span>{{
              $t('user.discountMsg')
                .replace('PRICE', scope.row.coupon.cashCondition)
                .replace('FOLD', scope.row.coupon.couponDiscount)
            }}</span>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      v-if="dataList.length"
      :current-page="page.currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="page.pageSize"
      :total="page.total"
      layout="total, sizes, prev, pager, next, jumper"
      @update:page-size="onPageSizeChange"
      @update:current-page="onPageChange"
    />
    <!-- /分页 -->
  </div>
</template>

<script setup>
const status = ref()
onMounted(() => {
  onGetData()
})

const options = [
  {
    value: 0,
    label: $t('user.invalid')
  },
  {
    value: 1,
    label: $t('user.notUsed')
  },
  {
    value: 2,
    label: $t('user.used')
  }
]
const defaultPage = {
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
}
const page = ref({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
let userId = ''
const dataList = ref([])
const init = id => {
  userId = id
  Object.assign(page.value, defaultPage)
  onGetData(page.value)
}
// 获取数据
const onGetData = pageParam => {
  if (!userId) {
    return
  }
  http({
    url: http.adornUrl('/admin/coupon/pageByUserId'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: pageParam == null ? page.value.currentPage : pageParam.currentPage,
          size: pageParam == null ? page.value.pageSize : pageParam.pageSize
        },
        {
          userId,
          status: status.value
        }
      )
    )
  }).then(({ data }) => {
    dataList.value = data.records
    page.value.total = data.total
  })
}
// 每页数
const onPageSizeChange = val => {
  page.value.pageSize = val
  page.value.currentPage = 1
  onGetData(page.value)
}
// 当前页
const onPageChange = val => {
  page.value.currentPage = val
  onGetData(page.value)
}
const statusChange = () => {
  page.value.currentPage = 1
  onGetData(page.value)
}
defineExpose({ init })
</script>

<style lang="scss" scoped>
.coupon-detail {
  margin-bottom: 10px;
}
</style>
